﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Globalization;
using System.Collections.Generic;

namespace Beetle.Sl.EditorControl.Controls
{
    public class FontFamilyInfosTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == null)
            {
                throw new ArgumentNullException("sourceType");
            }

            return ((sourceType == typeof(string)) || typeof(FontFamilyInfo[]).IsAssignableFrom(sourceType));
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return ((destinationType == typeof(string)) || (destinationType == typeof(FontFamilyInfo[])));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return null;
            }

            string str = value as string;
            List<FontFamilyInfo> fontFamilyInfos = null;

            if (str == null)
            {
                FontFamilyInfo[] fontFamilyInfoArr = value as FontFamilyInfo[];
                if (fontFamilyInfoArr == null)
                {
                    throw new NotSupportedException(string.Format("not support {0}", value));
                }

                return fontFamilyInfoArr;
            }

            if (string.IsNullOrEmpty(str))
            {
                return null;
            }

            string[] names = str.ToString().Split(new char[] { ',' });

            fontFamilyInfos = new List<FontFamilyInfo>();

            for (int i = 0; i < names.Length; i++)
            {
                string[] splits = names[i].Split(new char[] { ':' });

                if (splits.Length == 2)
                {
                    fontFamilyInfos.Add(new FontFamilyInfo()
                    {
                        Name = splits[0],
                        Description = splits[1],
                        Visibility = Visibility.Collapsed
                    });
                }
            }

            return fontFamilyInfos.ToArray();
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            FontFamilyInfo[] fontFamilyInfos = value as FontFamilyInfo[];
            if (fontFamilyInfos != null)
            {
                string[] arr = new string[fontFamilyInfos.Length];

                for (int i = 0; i < fontFamilyInfos.Length; i++)
                {
                    arr[i] = fontFamilyInfos[i].Name + ":" + fontFamilyInfos[i].Description;
                }

                return string.Join(",", arr);
            }

            return string.Empty;
        }
    }
}
